package org.litespring.beans.factory.support;

import org.litespring.beans.BeanDefinition;
import org.litespring.beans.factory.BeanCreationException;
import org.litespring.beans.factory.BeanFactory;
import org.litespring.util.ClassUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * DefaultBeanFactory实现BeanFactory ,BeanDefinitionRegistry接口
 * 实现其中的方法
 */
public class DefaultBeanFactory implements BeanFactory, BeanDefinitionRegistry {

    private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(64);
    private ClassLoader beanClassLoader;

    //private ClassLoader beanClassLoader;
//    public DefaultBeanFactory(String configFile) {
//        loadBeanDefiniton(configFile);
//
//    }
    public DefaultBeanFactory() {
    }

    //注册bean定义对象
    public void registerBeanDefinition(String beanID, BeanDefinition bd) {
        this.beanDefinitionMap.put(beanID, bd);
    }

    //获取beanID
    public BeanDefinition getBeanDefinition(String beanID) {
        return this.beanDefinitionMap.get(beanID);
    }

    //获取bean对象
    public Object getBean(String beanID) {
        BeanDefinition bd = this.getBeanDefinition(beanID);
        if (bd == null) { //没有这个id的bean
            throw new BeanCreationException("Bean Definition does not exist");
        }
        ClassLoader cl = ClassUtils.getDefaultClassLoader(); //当前线程的类加载器
        String beanClassName = bd.getBeanClassName();
        try {
            Class<?> clz = cl.loadClass(beanClassName);
            return clz.newInstance(); //有缺省的无参构造 没有事如何创建??
        } catch (Exception e) {
            throw new BeanCreationException("create Bean for" + beanClassName + "failed");
        }
    }

    public void setBeanClassLoader(ClassLoader beanClassLoader) {
        this.beanClassLoader = beanClassLoader;
    }


    /**
     * 解析xml文件成inputStream文件
     */
//    private void loadBeanDefiniton(String configFile) {
//        InputStream is = null;
//        try {
//            //读取xml文件
//            ClassLoader cl = ClassUtils.getDefaultClassLoader();
//            is = cl.getResourceAsStream(configFile);
//            SAXReader reader = new SAXReader();
//            Document doc = reader.read(is);
//
//            Element root = doc.getRootElement(); //获取beans标签
//            Iterator<Element> iter = root.elementIterator();
//            while (iter.hasNext()) {
//                Element ele = iter.next();
//                String id = ele.attributeValue(ID_ATTRIBUTE); //bean id
//                String beanClassName = ele.attributeValue(CLASS_ATTRIBUTE);
//                BeanDefinition bd = new GenericBeanDefinition(id, beanClassName); //生成bean实例
//                this.beanDefinitionMap.put(id, bd); //将bean放入实例
//            }
//
//        } catch (DocumentException e) {
//            throw new BeanDefinitionStoreException("IOException parsing XML Document", null);
//        } finally {
//
//            if (is != null) {
//                try {
//                    is.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        } //finally
//
//    }
//

    //    public void registerBeanDefinition(String beanID,BeanDefinition bd){
//        this.beanDefinitionMap.put(beanID, bd);
//    }



//    private Object createBean(BeanDefinition bd) {
//        ClassLoader cl = this.getBeanClassLoader();
//        String beanClassName = bd.getBeanClassName();
//        try {
//            Class<?> clz = cl.loadClass(beanClassName);
//            return clz.newInstance();
//        } catch (Exception e) {
//            throw new BeanCreationException("create bean for " + beanClassName + " failed", e);
//        }
//    }
//
//    public void setBeanClassLoader(ClassLoader beanClassLoader) {
//        this.beanClassLoader = beanClassLoader;
//    }
//
//    public ClassLoader getBeanClassLoader() {
//        return (this.beanClassLoader != null ? this.beanClassLoader : ClassUtils.getDefaultClassLoader());
//    }
}
